package com.zxy.furns.web;/**
 * ClassName: customerFurnServlet
 * Package: com.zxy.furns.web
 *
 * @Author zxy
 * @Create 2025/3/22 18:40
 * @Version 1.0
 * title:
 * Description:
 */

import com.zxy.furns.entity.Furn;
import com.zxy.furns.entity.Page;
import com.zxy.furns.service.FurnService;
import com.zxy.furns.service.impl.FurnServiceImpl;
import com.zxy.furns.utils.DataUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/customerFurnServlet")
public class CustomerFurnServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();


    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Furn> page = furnService.page(pageNo, pageSize);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }

    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("nameSearch");
        if (null == name) {
            name = "";
        }
        //这里我们对检索后，携带上次的条件做一个处理


        System.out.println("名字处理机制..");
        Page<Furn> page = furnService.pageByName(pageNo, pageSize, name);
        StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
        if (!"".equals(name)) {
            url.append("&nameSearch=" + name);
        }
        page.setUrl(url.toString());
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }
}